<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class Wallet extends Model
{

	protected $table = 'wallet';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = ['openid','uid','money'];

      /*
  *是否有记录登录信息
  */
    public function isLogin($openid)
    {
        $info = Member::where('open_id',$openid)->first();
        if ($info) {
            return $info;
        }
        return false;
    }

    //如果余额不存在去执行这里注册
    public function createUser($openid,$uid)
    {
        //firstOrCreate判断用如果openid不存在则写入新数据//这里的检测要全部加入模型里的白名单
        $user_wallet = Wallet::firstOrCreate(
            [
                'openid' => $openid,
            ],

            [
                    'uid'=>$uid,
                    'openid'=>$openid,
            ]);

            if($user_wallet){
                $res = Wallet::where('openid', $openid)->select('money')->first();
                return $res->money;
            }

            return false;
    }

}
